package com.atguigu.day19.reference03;

import org.junit.Test;

import java.util.Optional;
import java.util.function.Supplier;
/*
解决空指针异常
  提供备选方案
 */
public class OptionalTest {
    @Test
    public void test0() {
       // String s = null;
        String s = "辛弃疾";
        Optional<String> optional = Optional.ofNullable(s);
        String orElse = optional.orElse("安琪拉");
        System.out.println("orElse = " + orElse);
        System.out.println("orElse.length() = " + orElse.length());
    }

    @Test
    public void test02(){

        //String s  =null;
        String s  ="吕布";

        Optional<String> optional = Optional.ofNullable(s);
     /*   String orElseGet = optional.orElseGet(new Supplier<String>() {
            @Override
            public String get() {
                return "妲己";
            }
        });*/
        String orElseGet = optional.orElseGet(() -> "妲己");
        System.out.println("orElseGet = " + orElseGet);
    }
}
